package com.iflytek.aisol.audit.iflytek.strategy.manage.handler.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONUtil;
import com.iflytek.aisol.audit.iflytek.strategy.manage.config.CustomException;
import com.iflytek.aisol.audit.iflytek.strategy.manage.entity.ChannelConfig;
import com.iflytek.aisol.audit.iflytek.strategy.manage.entity.FrChannelConfigServerInstance;
import com.iflytek.aisol.audit.iflytek.strategy.manage.entity.ServerInstance;
import com.iflytek.aisol.audit.iflytek.strategy.manage.handler.ChannelHandler;
import com.iflytek.aisol.audit.iflytek.strategy.manage.mapper.ChannelConfigMapper;
import com.iflytek.aisol.audit.iflytek.strategy.manage.mapper.ServiceConfigMapper;
import com.iflytek.aisol.audit.iflytek.strategy.manage.mapper.fr.FrChannelConfigServerInstanceMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Component
@RequiredArgsConstructor
public class CommonChannelHandler implements ChannelHandler {
    private final FrChannelConfigServerInstanceMapper frChannelConfigServerInstanceMapper;
    private final ServiceConfigMapper serviceConfigMapper;
    private final ChannelConfigMapper channelConfigMapper;
    @Override
    public void handle(ChannelConfig channelConfig) {
        //1 如果传的是普通的channel(ive，vdt，rit，llmAudit_RAG，llmAudit)，
        // 则写入级联表frChannelConfigServerInstanceMapper
        Long id = channelConfig.getId();
        //2 转map，取出key为server_id、rag_server_id和ive_server_id的value
        Map<String, Object> jsonMap = JSONUtil.toBean(channelConfig.getConfig(), Map.class);
        if(jsonMap.containsKey("server_id")){
            Object instanceId = jsonMap.get("server_id");
            // 校验配置的 serverId 在数据库中存不存在
            checkAndSaveServer((String)instanceId,id);
        }
        if(jsonMap.containsKey("rag_server_id")){
            Object instanceId = jsonMap.get("rag_server_id");
            checkAndSaveServer((String)instanceId,id);
        }
        if(jsonMap.containsKey("ive_server_id")){
            Object instanceId = jsonMap.get("ive_server_id");
            // 校验配置的 serverId 在数据库中存不存在
            checkAndSaveServer((String)instanceId,id);
        }
    }
    // 校验通过，可以插入级联表中 需要校验级联的服务和channel是启用状态
    //InstanceId对应表字段instance_id,channelConfigId对应主键
    private void checkAndSaveServer(String InstanceId, Long channelConfigId) {
        // 这里的InstanceId 建议换成数据库中的非主键的instance_id ,前端提交到后端服务id不建议使用主键id
        // 校验级联的服务和channel是启用状态
        if (serviceConfigMapper.findWithInstanceIdAndNoDeleteAndVersionMax(InstanceId).isEmpty()) {
            throw new CustomException("instanceId="+ InstanceId +"在数据库是未被启用，不能插入级联表");
        }
        List<ServerInstance> serverInstance = serviceConfigMapper.findWithInstanceIdAndNoDeleteAndVersionMax(InstanceId);
        if(serverInstance.get(0)==null){
            throw new CustomException("找不大版本号最大且未删除的server记录");
        }
        Long serverId = serverInstance.get(0).getId();
        // 插入级联表
        FrChannelConfigServerInstance frChannelConfigServerInstance = new FrChannelConfigServerInstance();
        frChannelConfigServerInstance.setChannelConfigId(channelConfigId);
        frChannelConfigServerInstance.setServerInstanceId(serverId);
        frChannelConfigServerInstanceMapper.save(frChannelConfigServerInstance);
    }

}